<template>
  <div>
    <el-card style="margin: 20px 0">
      <category-select @getCategoryId="getCategoryId" :disableSelect="scene != 0"></category-select>
    </el-card>
    <el-card>
      <div v-show="scene == 0">
        <el-button type="primary" icon="el-icon-plus" :disabled="!category3Id" @click="addSpu">添加SPU</el-button>
        <el-table :data="records" style="width: 100%" border>
          <el-table-column type="index" label="序号" width="80" align="center"> </el-table-column>
          <el-table-column prop="spuName" label="spu名称" width="width"> </el-table-column>
          <el-table-column prop="description" label="spu描述" width="width"> </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ row, $index }">
              <hint-button type="success" icon="el-icon-plus" size="mini" title="添加SKU" @click="addSku(row)"></hint-button>
              <hint-button type="warning" icon="el-icon-edit" size="mini" title="编辑SPU" @click="updateSpu(row)"></hint-button>
              <hint-button type="info" icon="el-icon-info" size="mini" title="查看当前SPU全部SKU列表" @click="checkSkuList(row)"></hint-button>
              <el-popconfirm :title="`确定删除${row.spuName}吗？`" @onConfirm="deleteSpu(row)">
                <hint-button type="danger" icon="el-icon-delete" size="mini" title="删除SKU" slot="reference"></hint-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination :current-page="page" :page-sizes="[3, 5, 10]" :page-size="limit" layout="prev, pager, next, jumper, ->, total, sizes" :total="total" @current-change="getSpuList" @size-change="handleSizeChange" style="text-align: center"> </el-pagination>
      </div>
      <spu-form v-show="scene == 1" @changeScene="changeScene" ref="spu"></spu-form>
      <sku-form v-show="scene == 2" @changeScenes="changeScenes" ref="sku"></sku-form>
    </el-card>
    <el-dialog :title="`${spu.spuName}的SKU列表`" :visible.sync="dialogTableVisible" :before-close="close">
      <el-table :data="skuList" style="width: 100%" v-loading="loading">
        <el-table-column prop="skuName" label="名称" width="width"> </el-table-column>
        <el-table-column prop="price" label="价格" width="width"> </el-table-column>
        <el-table-column prop="weight" label="重量" width="width"> </el-table-column>
        <el-table-column label="默认图片" width="width">
          <template slot-scope="{row, $index}">
            <img :src="row.skuDefaultImg" alt="" style="width: 100px; height: 100px">
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import SpuForm from './SpuForm'
import SkuForm from './SkuForm'

export default {
  name: 'Spu',
  components: {
    SpuForm,
    SkuForm
  },
  data() {
    return {
      category1Id: '',
      category2Id: '',
      category3Id: '',
      page: 1,
      limit: 3,
      total: 23,
      records: [], //SPU列表数据
      scene: 0,
      dialogTableVisible: false,
      spu: '', //SPU数据
      skuList: [], //SKU列表数据
      loading: true
    }
  },
  methods: {
    handleCurrentChange(page) {},
    getCategoryId({ categoryID, level }) {
      if (level == 1) {
        this.category1Id = categoryID
        this.category2Id = ''
        this.category3Id = ''
      } else if (level == 2) {
        this.category2Id = categoryID
        this.category3Id = ''
      } else {
        this.category3Id = categoryID
        this.getSpuList()
      }
    },
    async getSpuList(pages = 1) {
      this.page = pages
      const { page, limit, category3Id } = this
      let result = await this.$API.spu.reqSpuList(page, limit, category3Id)
      if (result.code === 200) {
        this.total = result.data.total
        this.records = result.data.records
      }
    },
    handleSizeChange(limit) {
      this.limit = limit
      this.getSpuList()
    },
    // 添加SPU
    addSpu() {
      this.scene = 1
      this.$refs.spu.addSpuData(this.category3Id)
    },
    // 更新SPU
    updateSpu(row) {
      this.scene = 1
      this.$refs.spu.initSpuForm(row)
    },
    // 删除SPU
    async deleteSpu(row) {
      let result = await this.$API.spu.reqDeleteSpu(row.id)
      if (result.code == 200) {
        this.$message({ type: 'success', message: '删除成功' })
        this.getSpuList(this.records.length > 1 ? this.page : this.page - 1)
      }
    },
    // 自定义事件(切换场景)(from:SPU子组件)
    changeScene({ scene, flag }) {
      this.scene = scene
      if (flag == '修改') {
        this.getSpuList(this.page)
      } else {
        this.getSpuList()
      }
    },
    // 添加SKU
    addSku(row) {
      this.scene = 2
      this.$refs.sku.getData(this.category1Id, this.category2Id, row)
    },
    // 自定义事件（切换场景）(from:SKU子组件)
    changeScenes(scene) {
      this.scene = scene
    },
    // 查看SKU列表
    async checkSkuList(row) {
      this.spu = row
      this.dialogTableVisible = true
      let result = await this.$API.spu.reqSkuList(row.id)
      if (result.code == 200) {
        this.skuList = result.data
        this.loading = false
      }
    },
    // 关闭对话框的回调
    close(done) {
      // loading的属性再次变为真
      this.loading = true
      // 清除数据
      this.skuList = []
      // 关闭对话框
      done()
    }
  }
}
</script>

<style   scoped></style>
